# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\PyInstaller\archive\pyz_crypto.py
import os

BLOCK_SIZE = 16


def import_aes(module_name):
    """
    Tries to import the AES module from PyCrypto.
    
    PyCrypto 2.4 and 2.6 uses different name of the AES extension.
    """
    return __import__(module_name, fromlist=[module_name.split(".")[-1]])


def get_crypto_hiddenimports():
    """
    These module names are appended to the PyInstaller analysis phase.
    :return: Name of the AES module.
    """
    try:
        modname = "Crypto.Cipher._AES"
        import_aes(modname)
    except ImportError:
        modname = "Crypto.Cipher.AES"
        import_aes(modname)

    return modname


class PyiBlockCipher(object):
    """
    This class is used only to encrypt Python modules.
    """

    def __init__(self, key=None):
        if not type(key) is str:
            raise AssertionError
        if len(key) > BLOCK_SIZE:
            self.key = key[0:BLOCK_SIZE]
        else:
            self.key = key.zfill(BLOCK_SIZE)
        if not len(self.key) == BLOCK_SIZE:
            raise AssertionError
        self._aesmod = import_aes(get_crypto_hiddenimports())

    def encrypt(self, data):
        iv = os.urandom(BLOCK_SIZE)
        return iv + self._PyiBlockCipher__create_cipher(iv).encrypt(data)

    def __create_cipher(self, iv):
        return self._aesmod.new(self.key, self._aesmod.MODE_CFB, iv)
